﻿using System.Data;
using System.Data.SqlClient;

namespace Adams.Web.Data.Announcements
{
    public static class Index
    {
        public static Models.Announcements.Index.PageLoad PageLoad()
        {
            var data = new Models.Announcements.Index.PageLoad() { 
                Announcements = new Shared.Entities.Announcements.AnnouncementCollection()
            };
            
            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT A.*
FROM Announcements.Announcements A
WHERE DATEDIFF(d, GetDate(), A.EndDate) > 0 OR DATEDIFF(d, GetDate(), A.StartDate) > 0
", CommandType.Text))
            {
                while (dr.Read())
                {
                    var announcement = DB.InitFromReader<Shared.Entities.Announcements.Announcement>(dr, new ColMap { NameInDb = "AnnouncementID", NameInObject = "ID" });
                    data.Announcements.Add(announcement);
                }
            }

            return data;
        }
    }
}
